from flask import Flask, render_template, request
from flask import escape
import weather
from log_sys import log_request
app = Flask(__name__)

@app.route('/weathersearch', methods=['POST'])
# 这些数据来自哪里？
def do_search() -> 'html':
    key1 = "5ae4d72387a97e8529903ac948ab9eff"
    color1 = request.form['color1']
    color2 = request.form['color2']
    city = request.form['city']
    title = '这里是占卜的结果：'
    results = str(weather.weather1(key1, city))
    log_request(request,results)

    return render_template('results.html',
                           the_title=title,
                           the_city=city,
                           the_weather=results,
                           the_results=results,
                           the_color1=color1,
                           the_color2=color2,)

@app.route('/temperature', methods=['POST'])
# 这些数据来自哪里？
def wendu_search() -> 'html':
    key1 = "5ae4d72387a97e8529903ac948ab9eff"
    color1 = request.form['color1']
    color2 = request.form['color2']
    city = request.form['city']
    title = '这里是占卜的结果：'
    results = str(weather.temperature(key1, city))
    log_request(request,results)

    return render_template('results.html',
                           the_title=title,
                           the_city=city,
                           the_weather=results,
                           the_results=results,
                           the_color1=color1,
                           the_color2=color2,)

@app.route('/winddirection', methods=['POST'])
# 这些数据来自哪里？
def winddirection_search() -> 'html':
    key1 = "5ae4d72387a97e8529903ac948ab9eff"
    color1 = request.form['color1']
    color2 = request.form['color2']
    city = request.form['city']
    title = '这里是占卜的结果：'
    results = str(weather.winddirection(key1, city))
    log_request(request,results)

    return render_template('results.html',
                           the_title=title,
                           the_city=city,
                           the_weather=results,
                           the_results=results,
                           the_color1=color1,
                           the_color2=color2,)

@app.route('/windpower', methods=['POST'])
# 这些数据来自哪里？
def fengli_search() -> 'html':
    key1 = "5ae4d72387a97e8529903ac948ab9eff"
    color1 = request.form['color1']
    color2 = request.form['color2']
    city = request.form['city']
    title = '这里是占卜的结果：'
    results = str(weather.windpower(key1, city))
    log_request(request,results)

    return render_template('results.html',
                           the_title=title,
                           the_city=city,
                           the_weather=results,
                           the_results=results,
                           the_color1=color1,
                           the_color2=color2,)

@app.route('/humidity', methods=['POST'])
# 这些数据来自哪里？
def shidu_search() -> 'html':
    key1 = "5ae4d72387a97e8529903ac948ab9eff"
    color1 = request.form['color1']
    color2 = request.form['color2']
    city = request.form['city']
    title = '这里是占卜的结果：'
    results = str(weather.humidity(key1, city))
    log_request(request,results)

    return render_template('results.html',
                           the_title=title,
                           the_city=city,
                           the_weather=results,
                           the_results=results,
                           the_color1=color1,
                           the_color2=color2,)

@app.route('/quanbu', methods=['POST'])
# 这些数据来自哪里？
def quanbu_search() -> 'html':
    key1 = "5ae4d72387a97e8529903ac948ab9eff"
    color1 = request.form['color1']
    color2 = request.form['color2']
    city = request.form['city']
    title = '这里是占卜的结果：'
    results = str(weather.quanbu(key1, city))
    log_request(request,results)

    return render_template('all.html',
                           the_title=title,
                           the_city=city,
                           the_weather=results,
                           the_results=results,
                           the_color1=color1,
                           the_color2=color2,)

@app.route('/viewlog')
def view_the_log() -> 'html':
    """display the contents of thr log file as a html table"""
    contents = []
    with open('weather.log','r') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    len(contents)
    titles = ('用户提交','远程地址','访问代理','用户结果')
    return render_template('viewlog.html',
                           the_title='日志系统后台',
                           the_row_title=titles,
                           the_data=contents)


@app.route('/')
@app.route('/entry',methods=['GET','POST'])
def home():
    title = '欢迎来到天气占卜屋~'
    return render_template("home.html",
                           the_title=title,)

@app.route('/home')
def entry_page() -> 'html':
    title = '欢迎来到天气占卜屋~'
    return render_template('weather-search.html',
                           the_title=title)
if __name__ == '__main__':
    app.run()
